<?php
/**
 * Created by Benjiemin
 * Date: 2020/3/6
 * Time: 11:14
 */
require ('./vendor/autoload.php');
require ('./aip-php-sdk-2.2.15\AipOcr.php');
use QL\QueryList;
use QL\Ext\Chrome;

$ql = QueryList::getInstance();
// 注册插件，默认注册的方法名为: chrome
$ql->use(Chrome::class);
 $ql->chrome(function ($page,$browser) {
     $page->goto('https://www.discuz.net/forum.php');
     // 这里故意设置一个很长的延长时间，让你可以看到chrome浏览器的启动
     sleep(2);
     //输入关键词
     $username= 'tulouguli';
     $password = 'jian168@';
     $page->type("input[id='ls_username']",$username);
     $page->type("input[id='ls_password']",$password);
     $page->click("button[type='submit']");
     sleep(1);
     $pageImg = 'verify.jpeg';
     //获取页面截图
     $page->screenshot([
         'path' => $pageImg,
         'fullPage' => true
     ]);

     //验证码识别
     $appId = '9360309';
     $apiKey = 'f94FyaKX2LKx0GWBoddFVpxa';
     $secretKey = 'oGhM9QqHH6qslErF1LH8etP6Ber5adDz';
     $apiOcrModel = new AipOcr($appId, $apiKey, $secretKey);
     $verifyRet = $apiOcrModel->basicGeneral( file_get_contents($pageImg ));
     var_dump($verifyRet);die;

     if(!isset($verifyRet['words_result'][5]['words'])){
         echo '识别验证码失败';die;
     }
     $validateCode =  $verifyRet['words_result'][5]['words'];
     //等待搜索结果
     sleep(3);
     //获取结果
     $html = $page->content();
     //用jquery选择器抽取结果
     $rules = array(
         'title'=>['#content_left h3 a','text'],//标题
         'url'=>['#content_left h3 a','href'],//跳转网址
         'description'=>['div .c-abstract','text'],//描述
     );
     $ql = QueryList::html($html);
     $rt = $ql->rules($rules)->query()->getData();
     echo json_encode($rt);die;
     //如果有需要，可以把$rt入库，以及做其他操作
     sleep(10);
     $browser->close();
     // 返回值一定要是页面的HTML内容
     return $html;
 },[
     'headless' => false, // 启动可视化Chrome浏览器,方便调试
     'devtools' => false, // 打开浏览器的开发者工具
 ])->find('title')->text();



